package com.edu.tankgame;

import java.util.Vector;
//将敌方坦克做成线程，坐标自由移动
public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();
    //增加成员可以得到EnemyTank 敌人坦克的Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    boolean isLive = true;
    public EnemyTank(int x, int y) {
        super(x, y);
    }

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }
    //编写方法，判断当前的敌人坦克会碰到其他的坦克
    public boolean isTouchEnemyTank() {
        switch (this.getDirect()){
            case 0://上
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if(enemyTank != this){
                        if(enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //敌人的坦克是上和下
                                if(this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getX() <= enemyTank.getY() + 60){
                                    return true;
                                }
                                if (this.getX() + 40 >= enemyTank.getX()
                                &&  this.getX() + 40 <= enemyTank.getX() + 40
                                &&  this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60){
                                    return true;
                                }
                        }
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if(this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getX() <= enemyTank.getY() + 40){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    &&  this.getX() + 40 <= enemyTank.getX() + 60
                                    &&  this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1://右
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if(enemyTank != this){
                        if(enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //敌人的坦克是上和下
                            if(this.getX() +60 >= enemyTank.getX()
                                    && this.getX() +60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getX() <= enemyTank.getY() + 60){
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX()
                                    &&  this.getX() + 60 <= enemyTank.getX() + 40
                                    &&  this.getY() + 40>= enemyTank.getY()
                                    &&  this.getY() + 40 <= enemyTank.getY() + 60){
                                return true;
                            }
                        }
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if(this.getX()  + 60>= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getX() <= enemyTank.getY() + 40){
                                return true;
                            }
                            if (this.getX() + 60 >= enemyTank.getX()
                                    &&  this.getX() + 60 <= enemyTank.getX() + 60
                                    &&  this.getY() + 40 >= enemyTank.getY()
                                    && this.getY()  + 40 <= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2://下
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if(enemyTank != this){
                        if(enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //敌人的坦克是上和下
                            if(this.getX()  >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY()  + 60>= enemyTank.getY()
                                    && this.getX()  + 60<= enemyTank.getY() + 60){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    &&  this.getX() + 40 <= enemyTank.getX() + 40
                                    &&  this.getY() + 60>= enemyTank.getY()
                                    &&  this.getY() + 60 <= enemyTank.getY() + 60){
                                return true;
                            }
                        }
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if(this.getX() >= enemyTank.getX()
                                    && this.getX()  <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getX()  + 60<= enemyTank.getY() + 40){
                                return true;
                            }
                            if (this.getX() + 40 >= enemyTank.getX()
                                    &&  this.getX() + 40 <= enemyTank.getX() + 60
                                    &&  this.getY() + 60 >= enemyTank.getY()
                                    && this.getY()  + 60 <= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3://左
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if(enemyTank != this){
                        if(enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //敌人的坦克是上和下
                            if(this.getX()  >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getX() <= enemyTank.getY() + 60){
                                return true;
                            }
                            if (this.getX()  >= enemyTank.getX()
                                    &&  this.getX() <= enemyTank.getX() + 40
                                    &&  this.getY() + 40>= enemyTank.getY()
                                    &&  this.getY() + 40 <= enemyTank.getY() + 60){
                                return true;
                            }
                        }
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            if(this.getX() >= enemyTank.getX()
                                    && this.getX()  <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getX()<= enemyTank.getY() + 40){
                                return true;
                            }
                            if (this.getX()  >= enemyTank.getX()
                                    &&  this.getX() <= enemyTank.getX() + 60
                                    &&  this.getY() + 40 >= enemyTank.getY()
                                    && this.getY()  + 40 <= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }

    @Override
    public void run() {
        //这里判断如果shots size()==0  创建子弹
        while (true) {
            if(isLive && shots.size() == 0) {
                Shot s = null;
                //根据坦克的方向，创建对应的坦克
                switch (getDirect()){
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY() +20, 1);
                        break;
                    case 2:
                        s = new Shot(getX() + 20, getY() +60, 2);
                        break;
                    case 3:
                        s = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(s);
                new Thread(s).start();
            }
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if(getY() > 0 && !isTouchEnemyTank()) {
                            moveUp();
                        }
                        if(getY() == 0 && !isTouchEnemyTank()) {
                            break;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if(getX() + 60 < 1000 && !isTouchEnemyTank()) {
                            moveRight();
                        }
                        if(getX() + 60 == 1000 && !isTouchEnemyTank()) {
                            break;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if(getY() + 60 < 750 && !isTouchEnemyTank()) {
                            moveDown();
                        }
                        if(getY() + 60 == 750 && !isTouchEnemyTank()) {
                            break;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if(getX() > 0 && !isTouchEnemyTank()) {
                            moveLeft();
                        }
                        if(getX() == 0  && !isTouchEnemyTank()) {
                            break;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
            }
            //休眠

            setDirect((int)(Math.random()*4));
            if (!isLive) {
                break;
            }
        }
    }
}
